package stack

import (
	"strconv"
)

func evalRPN(tokens []string) int {

	stack := []int{}
	for _, s := range tokens {
		switch s {
		case "+":
			{
				num1 := stack[len(stack)-1]
				stack = stack[:len(stack)-1]
				num2 := stack[len(stack)-1]
				stack = stack[:len(stack)-1]
				stack = append(stack, num2+num1)
			}
		case "-":
			{
				num1 := stack[len(stack)-1]
				stack = stack[:len(stack)-1]
				num2 := stack[len(stack)-1]
				stack = stack[:len(stack)-1]
				stack = append(stack, num2-num1)
			}
		case "*":
			{
				num1 := stack[len(stack)-1]
				stack = stack[:len(stack)-1]
				num2 := stack[len(stack)-1]
				stack = stack[:len(stack)-1]
				stack = append(stack, num2*num1)
			}
		case "/":
			{
				num1 := stack[len(stack)-1]
				stack = stack[:len(stack)-1]
				num2 := stack[len(stack)-1]
				stack = stack[:len(stack)-1]
				stack = append(stack, num2/num1)
			}
		default:
			{
				num, _ := strconv.Atoi(s)
				stack = append(stack, num)
			}
		}
	}
	return stack[0]

}
